# 注册
import json

from aaa.api.base_api import BaseAPI


class Register(BaseAPI):
    def register(self, data_register):
        url_register = f'{self.BASE_URL}/login/register'
        request = self.session.post(url_register, data=data_register)
        return self._handle_response(request, '正常注册')

    def register_agin(self, data_register):
        url_register = f'{self.BASE_URL}/login/register'
        request = self.session.post(url_register, data=data_register)
        return self._handle_register_agin(request, '重复注册')

    def _handle_register_agin(self, response, action):
        try:
            response.encoding = 'utf-8'
            body = json.loads(response.text)
            print(f"{action}响应：{body}")
            if response.status_code == 200 and body.get('code') == 200:
                print(f"{action}成功")
                return True
            else:
                print(f"{action}失败：{body.get('msg', '未知错误')}")
                return False
        except json.JSONDecodeError:
            print(f"服务器未返回JSON格式，原始响应：{response.text}")
            return False
